package cn.wolfcode.shop.service.impl;

import cn.wolfcode.shop.domain.Good;
import cn.wolfcode.shop.domain.SeckillGood;
import cn.wolfcode.shop.exception.GlobalException;
import cn.wolfcode.shop.mapper.SeckillGoodMapper;
import cn.wolfcode.shop.redis.GoodPrefixKey;
import cn.wolfcode.shop.redis.RedisService;
import cn.wolfcode.shop.result.CodeMsg;
import cn.wolfcode.shop.service.IGoodService;
import cn.wolfcode.shop.service.ISeckillGoodService;
import cn.wolfcode.shop.vo.GoodVo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wolfcode-lanxw
 */
@Service
public class SeckillGoodServiceImpl implements ISeckillGoodService {
    @Autowired
    private SeckillGoodMapper seckillGoodMapper;
    @Reference
    private IGoodService goodService;
    @Autowired
    private RedisService redisService;
    @Override
    public List<GoodVo> query() {
        List<SeckillGood> seckillGoodList = seckillGoodMapper.query();
        List<GoodVo> goodVoList = getGoodVos(seckillGoodList);
        for(GoodVo goodVo:goodVoList){
            redisService.hset(GoodPrefixKey.GOOD_HASH,"",goodVo.getId()+"",goodVo);
        }
        return goodVoList;
    }
    @Override
    public GoodVo getById(Long goodId) {
        GoodVo vo = redisService.hget(GoodPrefixKey.GOOD_HASH,"",""+goodId,GoodVo.class);
       return vo;
    }

    @Override
    public int reduceStock(Long goodId) {
        int count = seckillGoodMapper.reduceStock(goodId);
        return count;
    }

    @Override
    public List<GoodVo> queryCache() {
        Map<String, GoodVo> goodVoMap = redisService.hgetAll(GoodPrefixKey.GOOD_HASH, "", GoodVo.class);
        ArrayList<GoodVo> goodVos = new ArrayList<>(goodVoMap.values());
        return goodVos;
    }

    @Override
    public int getGoodStockById(Long goodId) {
        return seckillGoodMapper.getGoodStockById(goodId);
    }

    private List<GoodVo> getGoodVos(List<SeckillGood> seckillGoodList) {
        Map<Long,SeckillGood> seckillGoodMap = new HashMap<>();
        for(SeckillGood sg:seckillGoodList){
            seckillGoodMap.put(sg.getGoodId(),sg);
        }
        List<Good> goodList = goodService.queryByIds(new ArrayList(seckillGoodMap.keySet()));
        List<GoodVo> goodVoList = new ArrayList<>();
        GoodVo vo;
        SeckillGood seckillGood;
        for(Good good:goodList){
            seckillGood = seckillGoodMap.get(good.getId());
            vo = new GoodVo();
            vo.setId(seckillGood.getId());
            vo.setGoodTitle(good.getGoodTitle());
            vo.setGoodStock(good.getGoodStock());
            vo.setGoodPrice(good.getGoodPrice());
            vo.setGoodName(good.getGoodName());
            vo.setGoodImg(good.getGoodImg());
            vo.setGoodDetail(good.getGoodDetail());
            vo.setStockCount(seckillGood.getStockCount());
            vo.setSeckillPrice(seckillGood.getSeckillPrice());
            vo.setStartDate(seckillGood.getStartDate());
            vo.setEndDate(seckillGood.getEndDate());
            goodVoList.add(vo);
        }
        return goodVoList;
    }
}
